package com.starblink.android.basic.base.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.CallSuper
import androidx.lifecycle.Observer
import com.starblink.android.basic.base.BaseViewModel
import com.starblink.android.basic.bean.LoadAction
import com.starblink.android.basic.widget.LoadingDialogManager
import com.starblink.android.basic.widget.gloading.Gloading

abstract class AbsLoadingBaseVMFragment<VM: BaseViewModel>: AbsVMFragment<VM>() {

    private lateinit var mGLoadingHolder: Gloading.Holder

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //观察网络数据状态
        viewModel.getActionLiveData().observe(viewLifecycleOwner, stateObserver)
        initData()
        initView(view)
        startObserve()
        initLoad()

    }

    open fun isDefaultShowLoading() = true

    //Loading Create Root View
    override fun transformRootView(view: View): View {
        mGLoadingHolder = generateGLoading(view)
        if (!isDefaultShowLoading()) {
            mGLoadingHolder.showLoadSuccess()
        }
        return mGLoadingHolder.wrapper
    }

    /**
     * 初始化数据
     */
    abstract fun initData()

    /**
     * 初始view
     */
    abstract fun initView(view: View)

    /**
     * 加载数据
     */
    abstract fun initLoad()

    abstract fun startObserve()


    //如果要替换GLoading，重写次方法
    private fun generateGLoading(view: View): Gloading.Holder = Gloading.getDefault().wrap(view).withRetry {
        onGoadingRetry()
    }

    protected open fun onGoadingRetry() {
    }


    // ================== 网络状态的监听 ======================

    private var stateObserver: Observer<LoadAction> = Observer { loadAction ->
        if (loadAction != null) {

            when (loadAction.action) {
                LoadAction.STATE_NORMAL -> showStateNormal()
                LoadAction.STATE_ERROR -> showStateError(loadAction.message)
                LoadAction.STATE_SUCCESS -> showStateSuccess()
                LoadAction.STATE_LOADING -> showStateLoading()
                LoadAction.STATE_NO_DATA -> showStateNoData()
                LoadAction.STATE_PROGRESS -> showStateProgress()
                LoadAction.STATE_HIDE_PROGRESS -> hideStateProgress()
            }

        }
    }

    protected open fun showStateNormal() {}

    protected open fun showStateLoading() {
        mGLoadingHolder.showLoading()
    }

    protected open fun showStateSuccess() {
        mGLoadingHolder.showLoadSuccess()
    }

    protected open fun showStateError(message: String?) {
        mGLoadingHolder.showLoadFailed(message)
    }

    protected open fun showStateNoData() {
        mGLoadingHolder.showEmpty(emptyMessage())
    }

    protected fun showStateProgress() {
        LoadingDialogManager.get().showLoading(mActivity)
    }

    protected fun hideStateProgress() {
        LoadingDialogManager.get().dismissLoading()
    }

    protected open fun emptyMessage(): String? = null
}